import 'dart:io';

import 'package:fire_chat/ui/start_page.dart';
import 'package:fire_chat/utils/localizations.dart';
import 'package:fire_chat/utils/sputils/sp_utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:get/get.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  MyApp({Key? key}) : super(key: key) {
    if (Platform.isAndroid) {
      // 以下两行 设置android状态栏为透明的沉浸。写在组件渲染之后，是为了在渲染后进行set赋值，覆盖状态栏，写在渲染之前MaterialApp组件会覆盖掉这个值。
      SystemUiOverlayStyle systemUiOverlayStyle = const SystemUiOverlayStyle(statusBarColor: Colors.transparent);
      SystemChrome.setSystemUIOverlayStyle(systemUiOverlayStyle);
    }
    _init();
  }

  _init() async {
    WidgetsFlutterBinding.ensureInitialized();
    //强制只能竖屏
    SystemChrome.setPreferredOrientations([DeviceOrientation.portraitUp, DeviceOrientation.portraitDown]);
  }

  @override
  Widget build(BuildContext context) {
    //初始化 sp
    SpUtils().init();
    // SystemChrome.setEnabledSystemUIOverlays([]); //隐藏状态栏，底部按钮栏localizations.dart
    return AnnotatedRegion<SystemUiOverlayStyle>(
      value: SystemUiOverlayStyle.dark,
      child: GetMaterialApp(
          title: '火讯',
          debugShowCheckedModeBanner: false,
          //防止长按输入框异常
          localizationsDelegates: const [
            //防止长按输入框异常
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
            ChineseCupertinoLocalizations.delegate,
          ],
          supportedLocales: const [Locale('zh', 'CN'), Locale('en', 'US')],
          theme: ThemeData(
              brightness: Brightness.light,
              backgroundColor: Colors.white),
          // home: FutureBuilder(
          //   future: _isFirst(),
          //   builder: (BuildContext context, AsyncSnapshot<bool> snapshot) {
          //     /*表示数据成功返回*/
          //     if (snapshot.hasData) {
          //       if (snapshot.data) {
          //         return StartPage();
          //       } else {
          //         return OnePage();
          //       }
          //     } else {
          //       return Container();
          //     }
          //   },
          // ),
          home: const StartPage(),
          builder: (BuildContext? context, Widget? child) {
            return MediaQuery(
              //设置文字大小不随系统设置改变
              data: MediaQuery.of(context!).copyWith(textScaleFactor: 0.9),
              child: child!,
            );
          }),
    );
  }

  Future<bool?> _isFirst() async {
    await SpUtils().init();
    return SpUtils().getBool("xieyiApp");
  }
}
